package com.project.controller.web;

import com.project.common.validator.ValidatorUtils;
import com.project.common.validator.group.AddGroup;
import com.project.entity.Kpoint;
import com.project.entity.User;
import com.project.myBeans.R;
import com.project.service.KpointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import com.project.common.utils.MyPageHelper;

import java.util.ArrayList;
import java.util.List;

@EnableAutoConfiguration
@RestController
@RequestMapping("/kpoint")
@Api(description = "知识点相关接口",value = "kpoint")
public class KpointController {

    @Autowired
    KpointService kpointService;

    /**
     * wwh 2018/3/24
     * @param kpoint
     * @return
     */
    @ApiOperation("添加知识点")
    @PostMapping("/add")
    public R add(@RequestBody Kpoint kpoint){
        try{
            if(kpoint.getParentid()==null){
                kpoint.setParentid(-1);
            }
            if(kpointService.findByCondition(kpoint).size()!=0){
                return R.error("该知识点已存在");
            }
            kpointService.add(kpoint);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("删除知识点（若为父节点则连带子节点删除）")
    @GetMapping("/removeById/{id}")
    public R removeById(@PathVariable Integer id){
        try{
            Kpoint kpoint = kpointService.findById(id);
            if(kpoint.getParentid()==-1){
                Kpoint kpoint1 = new Kpoint();
                kpoint1.setParentid(id);
                List<Kpoint> list = kpointService.findByCondition(kpoint1);
                for(Kpoint kpoint2 : list){
                    kpointService.removeById(kpoint2.getId());
                }
            }
            kpointService.removeById(id);
            return R.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * wwh 2018/05/16
     * @param courseid
     * @return
     */
    @ApiOperation("根据课程id返回知识点树")
    @GetMapping("/getKpointTree/{courseid}")
    public R navTree(@PathVariable Integer courseid){
        try{
            Kpoint kpoint = new Kpoint();
            kpoint.setCourseid(courseid);
            kpoint.setParentid(-1);
            List<Kpoint> kpointList = kpointService.findByCondition(kpoint);
            for (Kpoint kpoint1:kpointList){
                kpoint1.setChildren(kpointService.queryListByPid(kpoint1.getId()));
            }
            return R.ok().put("data",kpointList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.toString());
        }
    }

    /**
     * wqd 2018/4/11
     * @param kpoint
     * @return
     */
    @ApiOperation("修改知识点树")
    @PostMapping("kpoint/modify")
    public R modify(@RequestBody Kpoint kpoint) {
        ValidatorUtils.validateEntity(kpoint, AddGroup.class);
        kpointService.modify(kpoint);
        return R.ok();
    }

    /**
     * wqd 2018/04/15
     * @param kpoint
     * @return
     */
    @ApiOperation("条件查找")
    @PostMapping("/findByCondition")

    public R findByCondition(@RequestBody Kpoint kpoint){
        try{
            List<Kpoint> kpointList = kpointService.findByCondition(kpoint);
            return R.ok().put("data",kpointList).put("total", MyPageHelper.getTotal(kpointList));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    /**
     * Tate 2018/05/23
     *
     * @param kpointid
     * @param kpointname
     * @return
     */
    @ApiOperation("修改知识点名")
    @PostMapping("/modfiy/{kpointid}/{kpointname}")

    public R modfiy(@PathVariable Integer kpointid, @PathVariable String kpointname) {
        if (kpointService.changenamebyid(kpointid, kpointname) > 0) return R.ok("修改成功!");
        else
            return R.error();
    }
}

